﻿using System.Collections.Generic;
using System.Xml;
using System;

namespace ZJGIS.ManageUser
{
    /// <summary>
    /// 
    /// </summary>
    public interface IResourceNode
    {
        /// <summary>
        /// 资源名称
        /// </summary>
        string Name { get; set; }
        /// <summary>
        /// 资源别名
        /// </summary>
        string AliasName { get; set; }
        /// <summary>
        /// 资源是否允许使用
        /// </summary>
        bool Enable { get; set; }
        /// <summary>
        /// 子节点
        /// </summary>
        List<IResourceNode> ChildNodes { get; }
        /// <summary>
        /// 序列化为XML
        /// </summary>
        /// <param name="xmlDoc"></param>
        /// <returns></returns>
        XmlElement ToXmlNode(XmlDocument xmlDoc);
    }

    /// <summary>
    /// 
    /// </summary>
    public abstract class ResourceNode : IResourceNode
    {
        /// <summary>
        /// 
        /// </summary>
        public ResourceNode()
        {
            this.Enable = false;
            this.ChildNodes = new List<IResourceNode>();
        }

        /// <summary>
        /// 资源名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 资源是否允许使用
        /// </summary>
        public bool Enable { get; set; }

        /// <summary>
        /// 子节点
        /// </summary>
        public List<IResourceNode> ChildNodes { get; set; }

        /// <summary>
        /// 别名
        /// </summary>
        public string AliasName { get; set; }

        /// <summary>
        /// 序列化为XML
        /// </summary>
        /// <param name="xmlDoc"></param>
        /// <returns></returns>
        public abstract XmlElement ToXmlNode(XmlDocument xmlDoc);
    }
    
    /// <summary>
    /// 
    /// </summary>
    public class UserResource : ResourceNode
    {
        /// <summary>
        /// 
        /// </summary>
        public UserResource()
        {

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="xml"></param>
        /// <returns></returns>
        public bool InitUserResourceFromXml(string xml)
        {
            XmlDocument xmlDoc = new XmlDocument();
            try
            {
                xmlDoc.LoadXml(xml);
            }
            catch
            {
                xmlDoc.LoadXml("<x/>");
            }

            XmlNode rootNode = xmlDoc.SelectSingleNode("//Resource");
            if (rootNode == null)
            {
                return false;
            }
            XmlNodeList nodeList = rootNode.ChildNodes;
            foreach (XmlNode node in nodeList)
            {
                IResourceNode rNode = NodeSerializer.FromXmlNode(node);
                if (rNode != null)
                {
                    ChildNodes.Add(rNode);
                }
            }
            return true;

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="xmlDoc"></param>
        /// <returns></returns>
        public override XmlElement ToXmlNode(XmlDocument xmlDoc)
        {
            XmlElement xmlEle = xmlDoc.CreateElement("Resource");
            xmlDoc.AppendChild(xmlEle);
            foreach (IResourceNode node in ChildNodes)
            {
                XmlElement xmlEle1 = node.ToXmlNode(xmlDoc);
                if (xmlEle1 != null)
                {
                    xmlEle.AppendChild(xmlEle1);
                }
            }
            return xmlEle;
        }
    }
}
